<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Loader;

class Base extends Controller
{
    public function __construct(){
        parent::__construct();
        cookie(['prefix' => '', 'expire' => 60*60*24]);
        $this->token = md5(time());
        $this->assign('token',$this->token);

        //推荐
        $fid = input('get.fid');
        if($fid){
            $_SESSION['fid'] = $fid;
            if(!isset($_SESSION['uid'])){
                $this->redirect('login/register?token='.$this->token);
            }
        }



        //session_unset();
        //验证登录
        // if(!iswechat()){
        // 	$_SESSION['uid'] = 991;
        // }
        if(!isset($_SESSION['uid'])){

            //$this->error('请先登录！','index.php/index/user/login',1,1);
            $this->redirect('login/login?token='.$this->token);
        }

        $this->uid = $_SESSION['uid'];
        $this->user = db('userinfo')->where('uid',$this->uid)->find();
        if(!$this->user){
            unset($_SESSION['uid']);
            $this->redirect('login/login?token='.$this->token);
        }


        $this->assign('userinfo',$this->user);
        //网站配置信息
        $this->conf = getconf('');
        if($this->conf['is_close'] != 1){
            header('Location:/error.html');
            exit;
        }
        $this->assign('conf',$this->conf);

        if(empty($this->user['oid']) || empty($this->user['utel']) || empty($this->user['upwd'])){
            $this->redirect('login/addpwd?token='.$this->token);
        }
        $uid = $this->uid;;
        $user = Db::name('userinfo')->where('uid',$uid)->find();

        $this->assign('userinfo',$user);

        $order = db('order');
        $tody_starttime = strtotime(date("Y").'-'.date("m").'-'.date("d").' 00:00:00');
        $tody_endtime = strtotime(date("Y").'-'.date("m").'-'.date("d").' 24:00:00');
        $tody_ploss_map['uid'] = $uid;
        $tody_ploss_map['buytime'] = array('between time',array($tody_starttime,$tody_endtime));
        $all_ploss = $order->where($tody_ploss_map)->sum('ploss');
        //$tody_ploss_map['uid'] = $uid;
        // $all_ploss = $order->where($tody_ploss_map)->sum('ploss');
        $this->assign('all_ploss',$all_ploss);
        //充值方式
        $payment = db('payment')->where(array('isdelete'=>0,'is_use'=>1))->order('pay_order desc ')->select();
        if($payment){
            $arr2 = $arr = $arr1 = array();
            foreach ($payment as $key => $value) {


                $arr1 = explode('|',trimall($value['pay_conf']));

                foreach ($arr1 as $k => $v) {
                    $arr2 = explode(':',trimall($v));
                    if(isset($arr2[0]) && isset($arr2[1])){
                        $arr[$arr2[0]] = $arr2[1];
                    }


                }
                $payment[$key]['pay_conf_arr'] = $arr;


            }
        }
        $this->assign('payment',$payment);
       
      
        //test
        //dump($_SESSION);exit;

    }

    protected function fetch($template = '', $vars = [], $replace = [], $config = [])
    {
        $replace['__HOME__'] = str_replace('/index.php','',\think\Request::instance()->root()).'/static/index';
        return $this->view->fetch($template, $vars, $replace, $config);
    }
}
